'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 查询竞技场商城
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var PlayerManager = require('../../../../libs/playerManager');
var dataApi = require('../../../../libs/dataApi');
var roleService = require('../../../../services/roleService');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var refresh_shop = msg.refresh_shop;

    var player_manager = new PlayerManager(role_id);
    var shop_list = [];
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['arena', 'accrued'], cb);
            },
            function (cb) {
                if (refresh_shop !== 1) {
                    return cb();
                }

                // 刷新商城
                var role_model = player_manager.role;
                var arena_model = player_manager.arena;

                var next_refresh_times = arena_model.refresh_shop_times + 1;
                var refresh_conf = dataApi.shop_refresh.findById(next_refresh_times);
                if (!refresh_conf) {
                    return next(null, {
                        code: Code.SHOP_INVALID_REFRESH_TIMES,
                    });
                }

                // 扣除钻石
                var sub_diamond = refresh_conf.arena_shop;
                if (!roleService.subDiamond(player_manager, moduleType.MDL_ARENA, operationType.OPR_ARENA_REFRESH_SHOP, sub_diamond)) {
                    return next(null, {
                        code: Code.USER_DIAMOND_NOT_ENOUGH
                    });
                }

                // 刷新商城
                if (!arena_model.manulRefreshShop()) {
                    return next(null, {
                        code: Code.SHOP_NO_REFRESH_TIMES,
                    });
                }

                player_manager.save(cb);
            },
            function (cb) {
                var arena_model = player_manager.arena;
                _.forEach(arena_model.arena_shop, function (item) {
                    shop_list.push({
                        shop_pos: item.shop_pos,
                        shop_id: item.shop_id,
                        count: item.count
                    });
                });

                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    shop_list: shop_list
                }
            });
        }
    );
};
